/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Bsp.h"
#include "main.h"
#include "device_pins.h"
#include "xgzp6828d.h"
/* Extern variables --------------------------------------------------------------------*/
/* Private define ----------------------------------------------------------------------*/
#define DEV_ADDR    0XDA
/* Private macro -----------------------------------------------------------------------*/
/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/
/* Private functions -------------------------------------------------------------------*/



/*****************************************************************************************
* Function Name  : IO_uDelay
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
static void IIC_uDelay(uint16_t x)
{
	uint16_t i;
	while(x--)
	{
		for(i=0;i<1;i++);
	}
}
//

/*****************************************************************************************
* Function Name  : IO_IIC_Start
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void IIC_IIC_Start(void)
{	
	IIC_uDelay(2);
	PS0_SDA_H();
	IIC_uDelay(2);
	PS_SCL_H();
	IIC_uDelay(2);
	PS0_SDA_L();
	IIC_uDelay(2);
	PS_SCL_L();
}

//

/*****************************************************************************************
* Function Name  : IO_IIC_Stop
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void IIC_IIC_Stop(void)
{
	PS_SCL_L();
	IIC_uDelay(2);
	PS0_SDA_L();
	IIC_uDelay(2);
	PS_SCL_H();
	IIC_uDelay(2);
	PS0_SDA_H();
}

//

/*****************************************************************************************
* Function Name  : IO_WriteByte
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void IIC_WriteByte(uint8_t txd)
{
	uint8_t t;   	    
	PS_SCL_L();
	IIC_uDelay(2); 
	for(t=0;t<8;t++)
	{              
		if((txd&0x80))
		{
			PS0_SDA_H();
		}
		else
		{
			PS0_SDA_L();
		}
		txd<<=1; 

		IIC_uDelay(2);	  	
		PS_SCL_H();
		IIC_uDelay(5);
		PS_SCL_L(); 
		IIC_uDelay(2);	
	} 
}


//

/*****************************************************************************************
* Function Name  : IO_ReadByte
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
uint8_t IIC_ReadByte(uint8_t Ack)
{
	uint8_t t; 
	uint8_t Temp;
	PS_SCL_L();
	IIC_uDelay(5);

	Temp = 0;
	for(t=0;t<8;t++)
	{       
		Temp=Temp<<1;	

		PS_SCL_H();
		IIC_uDelay(5);

		if(PS0_SDA()!=0)
		{
			Temp |= 0x01;
		} 

		PS_SCL_L(); 
		IIC_uDelay(5);
	} 

	if(Ack != 0)
	{
		PS0_SDA_L();
	}
	else
	{
		PS0_SDA_H();
	}
	
	IIC_uDelay(5);
	PS_SCL_H();
	IIC_uDelay(5);
	PS_SCL_L();
	
	return 	Temp;
}

//

/*****************************************************************************************
* Function Name  : IO_WaitAck
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
uint8_t IIC_WaitAck(void)
{
	uint8_t ucErrTime=0;

	PS0_SDA_H();

	IIC_uDelay(2);
	PS_SCL_H();

	while(PS0_SDA())
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			IIC_IIC_Stop();
			return 1;
		}
	}

	IIC_uDelay(5);
	PS_SCL_L();
	return 0;  
}

//

/*****************************************************************************************
* Function Name  : IO_SendNAck
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
uint8_t IIC_SendNAck(void)
{

	PS0_SDA_H();	
	    
	IIC_uDelay(2);
	PS_SCL_H();
	IIC_uDelay(2);
	PS_SCL_L(); 
	return 0;  
}




uint8_t IIC_burst_read(uint8_t devaddr,uint8_t regaddr,uint8_t *buf,uint8_t len)
{
	uint8_t i;
	
    IIC_IIC_Start();
  
    IIC_WriteByte(devaddr&0xfe);
    if(IIC_WaitAck()) 
		return 1;
  
    IIC_WriteByte(regaddr);
    if(IIC_WaitAck()) 
		return 1;
  
    IIC_IIC_Start();
    IIC_WriteByte(devaddr|0x01);
    if(IIC_WaitAck()) 
		return 1;
  
    for(i=0;i<len-1;i++)
	{
        buf[i]=IIC_ReadByte(1);		
    }
    
	buf[i]=IIC_ReadByte(0);
    
    IIC_IIC_Stop();
    
    return 0;
    
}

uint8_t IIC_burst_write(uint8_t devaddr,uint8_t regaddr,uint8_t *buf,uint8_t len)
{
  	uint8_t i;
	
    IIC_IIC_Start();
  
    IIC_WriteByte(devaddr&0xfe);
    if(IIC_WaitAck()) 
		return 1;
  
    IIC_WriteByte(regaddr);
    if(IIC_WaitAck()) 
		return 1;
 
    for(i=0;i<len;i++)
	{
		IIC_WriteByte(buf[i]);	
		if(IIC_WaitAck()) 
			return 1;	
    }
    
    IIC_IIC_Stop();
    return 0;
}



uint8_t xgzp6828d_get_pressure(int *pres,int *temp)
{
	uint8_t result;
	uint8_t buf[3];
	uint32_t pressure,temperature;

	buf[0]=0x0a;
	result = IIC_burst_write(DEV_ADDR,0x30,buf,1);
	if(result)
		return result;

	vTaskDelay(pdMS_TO_TICKS(15));
	if(pres)
	{
		IIC_burst_read(DEV_ADDR,6,&buf[0],1);
		IIC_burst_read(DEV_ADDR,7,&buf[1],1);
		IIC_burst_read(DEV_ADDR,8,&buf[2],1);
		pressure = ((uint32_t)buf[0] <<16 )|((uint32_t)buf[1] <<8 )|((uint32_t)buf[2]);
			
		if(pressure & (1<<23))
		{
			pressure/=16;
			pressure|=0xFFF00000;
		}
		else
		{
			pressure/=16;
		}

		*pres = (int)pressure;
	}

	if(temp)
	{
		IIC_burst_read(DEV_ADDR,9,&buf[0],1);
		IIC_burst_read(DEV_ADDR,10,&buf[1],1);
		temperature = ((uint32_t)buf[0] <<8 )|((uint32_t)buf[1]);
		temperature = temperature*100/256;
		*temp = temperature;
	}
	return 0;
}

void pressure_get_A(int *v,int *t)
{
	int pressure1,temperature1;

	xgzp6828d_get_pressure(&pressure1,&temperature1);

	*v=pressure1/10;
	*t=temperature1;
}
//
int32_t pressure_test_A;
int32_t pressure_test_B;

int pressure_sen_polling(void)
{
	static uint16_t pressure_timer;
	
	if(pressure_timer++>4)
	{
		pressure_timer = 0;
		pressure_get_A((int *)(&pressure_test_A),(int *)(&pressure_test_B));
		
		iProfile.pressure.pressure_data = dpoint_cal(&iProfile.pressure.calib[0],pressure_test_A);
		iProfile.pressure.pressure_data_raw = pressure_test_A;
        
        iProfile.sys_status.v_data_buf[V_ID_VAC_PRES] =	iProfile.pressure.pressure_data;
	}
	
	return 0;
}


/*****************************************************************************************
* Function Name  : IIC_PortInit
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
uint8_t xgzp6828d_init(void)
{
	uint8_t res,buf[3];

	res = IIC_burst_read(DEV_ADDR,0XA5,buf,1);
	if(res) 
		return res;

	buf[0]=buf[0]&0xfd;
	res = IIC_burst_write(DEV_ADDR,0XA5,buf,1);

	return res;
}

